//
//  MJImageCache.m
//  MJ_ImageCache
//
//  Created by apple on 16/5/14.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "MJImageCache.h"

@interface MJImageCache ()

@property(nonatomic,retain)NSCache *cache;

@end

@implementation MJImageCache

- (void)dealloc{

    [_cache release];
    [super dealloc];
    
}

+ (instancetype)shareImageCache{

    static MJImageCache *imagecache = nil;
    
    if (imagecache == nil) {
       
        imagecache = [[MJImageCache alloc] init];
        
    }

    return imagecache;
}

#pragma mark - Creating and Init
- (instancetype)init{

    self = [super init];
    
    if (self) {
        
        if (self.cache == nil) {
            
            self.cache = [[NSCache alloc] init];
        
        }
        
    }

    return self;
}


#pragma mark - Interface Function

- (void)insertImageToCache:(UIImage *)image
                 key:(NSString *)key
                     pngYn:(BOOL)pngYn
                    toDisk:(BOOL)toDisk{

    if (!image||!key) {
        return;
    }
    
    //存入缓存区
    NSInteger cost = [self getImageCost:image];
    [self.cache setObject:image forKey:key cost:cost];
    
    if (toDisk) {
        
        NSData *imageData = [[NSData alloc] init];
        
        if (pngYn) {
            
            imageData = UIImagePNGRepresentation(image);
            
        }else{
            
            imageData = UIImageJPEGRepresentation(image, 1.0f);
            
        }
        
        NSString *pathOfCache = [self makePicCachePath:key];
        //存入缓存文件夹
        [imageData writeToFile:pathOfCache atomically:YES];
        
    }
    

}

- (void)insertImageOnlyToCache:(UIImage *)image
                           key:(NSString *)key
                         pngYn:(BOOL)pngYn{

    [self insertImageToCache:image key:key pngYn:pngYn toDisk:NO];

}


//最大成本
- (void)setCachetotalCostLimit:(NSUInteger)totalCostLimit{
    
    self.cache.totalCostLimit = totalCostLimit;
    
}

//最大数量
- (void)setCacheCountLimit:(NSUInteger)countLimit{
    
    self.cache.countLimit = countLimit;
    
}


- (UIImage *)selectImageFromCache:(NSString *)key{

    
    NSString *pathOfCache = [self makePicCachePath:key];
    
    //从缓存区读取
    UIImage *image = [self.cache objectForKey:key];
    
    if (image) {
        
        return image;
        
    }
    
    //Cache文件夹
    NSData *dataOfCache = [NSData dataWithContentsOfFile:pathOfCache];
    if (dataOfCache) {
        
        return [UIImage imageWithData:dataOfCache];
        
    }
    
    return nil;
}

- (void)removeImageFromCacheWithkey:(NSString *)key{

    [self.cache removeObjectForKey:key];
    
}

- (void)removeAllCache{

    [self.cache removeAllObjects];

}



#pragma mark - Help Function

- (NSString *)makePicCachePath:(NSString *)imageName{

    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];

    return [path stringByAppendingPathComponent:imageName];

}

//获取成本
- (NSInteger)getImageCost:(UIImage *)image{

    return image.size.width * image.size.height * image.scale * image.scale;

}


@end
